package com.ruoyi.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LsLawyer;
import com.ruoyi.system.service.ILsLawyerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 律师Controller
 *
 * @author zhouyipeng
 * @date 2023-10-16
 */
@RestController
@RequestMapping("/system/lawyer")
public class LsLawyerController extends BaseController
{

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private ILsLawyerService lsLawyerService;

    /**
     * 查询律师列表
     */
    @PreAuthorize("@ss.hasPermi('system:lawyer:list')")
    @GetMapping("/list")
    public TableDataInfo list(LsLawyer lsLawyer)
    {
        startPage();
        List<LsLawyer> list = lsLawyerService.selectLsLawyerList(lsLawyer);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:lawyer:listFromC')")
    @GetMapping("/listFromC")
    public TableDataInfo listFromC(LsLawyer lsLawyer)
    {
        startPage();
        List<LsLawyer> list = lsLawyerService.selectLsLawyerListFromC(lsLawyer);
        return getDataTable(list);
    }

    /**
     * 导出律师列表
     */
    @PreAuthorize("@ss.hasPermi('system:lawyer:export')")
    @Log(title = "律师", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LsLawyer lsLawyer)
    {
        List<LsLawyer> list = lsLawyerService.selectLsLawyerList(lsLawyer);
        ExcelUtil<LsLawyer> util = new ExcelUtil<LsLawyer>(LsLawyer.class);
        util.exportExcel(response, list, "律师数据");
    }

    /**
     * 获取律师详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:lawyer:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(lsLawyerService.selectLsLawyerById(id));
    }

    /**
     * 新增律师
     */
    @PreAuthorize("@ss.hasPermi('system:lawyer:add')")
    @Log(title = "律师", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LsLawyer lsLawyer)
    {
        return toAjax(lsLawyerService.insertLsLawyer(lsLawyer));
    }

    /**
     * 修改律师
     */
    @PreAuthorize("@ss.hasPermi('system:lawyer:edit')")
    @Log(title = "律师", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LsLawyer lsLawyer)
    {
        return toAjax(lsLawyerService.updateLsLawyer(lsLawyer));
    }

    /**
     * 删除律师
     */
    @PreAuthorize("@ss.hasPermi('system:lawyer:remove')")
    @Log(title = "律师", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(lsLawyerService.deleteLsLawyerByIds(ids));
    }

    @GetMapping("/getDefault")
    public AjaxResult getDefault(HttpServletRequest request,String basePath){
        //获取登录域名
        String basePaths = getBasePath(request);
        logger.info(basePaths+"---来源地址----"+basePath);
        //根据域名获取手机号
        LsLawyer aDefault = lsLawyerService.getDefault(basePath);
        if (null == aDefault){
            return error("未查询到首席律师信息");
        }

        return success(aDefault);
    }
}
